/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.panteleyev.blogclient;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.panteleyev.blogapi.IBlogEntry;

/**
 *
 * @author Petr Panteleyev
 */
public final class Previewer {
    private StringBuffer m_fullText;

    private File m_fullFile;

    public URL getFullFileURL() throws MalformedURLException {
        return m_fullFile.toURI().toURL();
    }

    /**
     * Creates a new instance of GBPreviewer
     * @param post
     * @throws java.io.IOException
     */
    public Previewer(IBlogEntry post) throws IOException {
        /* Open temporary files */
        m_fullFile = File.createTempFile("ljw", ".htm");
        m_fullFile.deleteOnExit();

        /* Parse subject line */
        StringBuilder subjectText = new StringBuilder(post.getSubject());
        StringBuilder textBuffer = new StringBuilder(post.getBody());

        m_fullText = new StringBuffer("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");

        /* Add some styles */
        StringBuilder st = new StringBuilder("<style type=\"text/css\">\n")
            .append("a { text-decoration: none; }\n")
            .append("body,table,tr,td,form {font-size: 12pt; font-family: Verdana,Verdana,Tahoma,Arial,sans-serif;}")
            .append("</style>");

        m_fullText.append(st.toString())
            .append("</head>")
            .append("<body vlink=\"#500050\" text=\"#000000\" bgcolor=\"#FFFFFF\" alink=\"#FF00C0\" link=\"#000050\" style=\"WORD-WRAP: break-word\">")
            .append("<h1>")
            .append(subjectText)
            .append("</h1>")
            .append(textBuffer.toString().replace("\n", "<br>"))
            .append("</body></html>");

        try (PrintWriter pr = new PrintWriter(m_fullFile, "UTF-8")) {
            pr.print(m_fullText);
        }
    }
}
